package com.intelligence.tio.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.json.Json;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.common.packages.KickUserReqBody;
import com.intelligence.tio.common.packages.KickUserRespBody;
import com.intelligence.tio.common.packages.UserExitReqBody;

/**
 * 
* @Title: KickUserReqHandler.java 
* @Package com.intelligence.tio.server.handler 
* @Description: 踢人消息处理器 
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月24日 下午4:28:53 
* @version V1.0
 */
public class KickUserReqHandler extends AbsTioBsHandler<KickUserReqBody>
{
	private static Logger log = LoggerFactory.getLogger(KickUserReqHandler.class);

	/** 
	 * 
	 */
	@Override
	public Class<KickUserReqBody> bodyClass()
	{
		return KickUserReqBody.class;
	}

	@Override
	public Object handler(JsonPacket packet, KickUserReqBody bsBody, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception
	{
		log.info("收到踢人请求消息:{}", Json.toJson(bsBody));

		//获取用户上下文对象
		TioSessionContext sessionContext = channelContext.getSessionContext();
		//获取被踢出用户信息
		ChannelContext<TioSessionContext, JsonPacket, Object> kickedUserdChannelContext = Aio.getChannelContextByUserid(channelContext.getGroupContext(), bsBody.getKickedUserId());
		TioSessionContext kickedUserdSessionContext = kickedUserdChannelContext.getSessionContext();
		String kickedUserName = kickedUserdSessionContext.getUserName();

		KickUserRespBody kickUserBody = new KickUserRespBody();
		JsonPacket respPacket = new JsonPacket();
		//踢出提示消息
		kickUserBody.setMsg(bsBody.getMsg());
		//踢出用户消息响应
		kickUserBody.setCommand(Command.COMMAND_KICKUSER_RESP.getValue());
		kickUserBody.setKickedUserId(bsBody.getKickedUserId());
		kickUserBody.setAdminId(sessionContext.getUserId());
		kickUserBody.setCode(1);
		kickUserBody.setKickUserName(kickedUserName);
		respPacket.setBody(Json.toJson(kickUserBody).getBytes(JsonPacket.CHARSET));
		//群发消息
		Aio.sendToGroup(channelContext.getGroupContext(), sessionContext.getGroupId(), respPacket);


		return null;
	}
}
